package com.example.wanbaqian.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.bean.ApplyTwoBean;
import com.example.wanbaqian.utils.JsonUtils;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/6 0006.
 * 额度申请第二部适配器
 */
public class ApplyTwoAdapter extends RecyclerView.Adapter<ViewHolder> implements View.OnClickListener {
    private List<ApplyTwoBean> list = new ArrayList<>();
    private Context context;
    private int head = 0x1111;
    private int item = 0x2222;
    private int foot = 0x3333;
    private Dialog dialog = null;
    private View dialogView;
    private CheckBox checkBox;
    String[] userAndBankStat = new String[2];

    public ApplyTwoAdapter(List<ApplyTwoBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public ApplyTwoAdapter(List<ApplyTwoBean> list, Context context, String[] userAndBankStat) {
        this.list = list;
        this.context = context;
        this.userAndBankStat = userAndBankStat;
    }
//    public void setContent(String content) {
//        this.content = content;
//    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == head)
            return new ViewHolder(context, LayoutInflater.from(context).inflate(R.layout.item_apply_two_head, parent, false));
        else if (viewType == item)
            return new ViewHolder(context, LayoutInflater.from(context).inflate(R.layout.item_apply_two_item, parent, false));
        else
            return new ViewHolder(context, LayoutInflater.from(context).inflate(R.layout.item_apply_two_foot, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (getItemViewType(position) == item) {
            holder.setImageResource(R.id.item_apply_twoIv, list.get(position - 1).getDrawableId());
            holder.setText(R.id.item_apply_twoTvTitle, list.get(position - 1).getTitle());
            holder.setText(R.id.item_apply_twoTvHint, list.get(position - 1).getHint());
            holder.setText(R.id.item_apply_twoTvStat, list.get(position - 1).getStat() + "(必填)");
            holder.setTextColorRes(R.id.item_apply_twoTvStat, list.get(position - 1).getStat().startsWith("已") ? R.color.colorBackground : R.color.colorTextGray);
            holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    applyTwoListeren.itemClick(position - 1);
                }
            });
        } else if (getItemViewType(position) == foot) {
//            checkBox = holder.getView(R.id.item_apply_twoCb);
//            TextView cancleTv = (TextView) dialogView.findViewById(R.id.dialog_cancle);
//            TextView titleTv = (TextView) dialogView.findViewById(R.id.dialog_title);
//            TextView contentTv = (TextView) dialogView.findViewById(R.id.dialog_content);
//            contentTv.setText(content);
//            Button okTv = (Button) dialogView.findViewById(R.id.dialog_ok);
//            cancleTv.setOnClickListener(ApplyTwoAdapter.this);
//            okTv.setOnClickListener(ApplyTwoAdapter.this);
//            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    //选中之后才show的dialog
//                    if (checkBox.isChecked())
//                        dialog.show();
//                    else
//                        applyTwoListeren.footClick(checkBox.isChecked());
//                }
//            });
            holder.setOnClickListener(R.id.apply_two_ok, ApplyTwoAdapter.this);
        } else {
            holder.setOnClickListener(R.id.apply_one_user, ApplyTwoAdapter.this);
            holder.setOnClickListener(R.id.apply_one_bank, ApplyTwoAdapter.this);
            holder.setText(R.id.apply_one_user, "身份认证(" + userAndBankStat[0] + ")");
            holder.setText(R.id.apply_one_bank, "添加银行卡(" + userAndBankStat[1] + ")");
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return head;
        } else if (position > 0 && position <= list.size()) {
            return item;
        } else {
            return foot;
        }
    }

    @Override
    public int getItemCount() {
        return list.size() + 2;
    }

    private ApplyTwoListeren applyTwoListeren;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_cancle:
                applyTwoListeren.footClick(false);
                checkBox.setChecked(false);
                dialog.dismiss();
                break;
            case R.id.dialog_ok:
                applyTwoListeren.footClick(true);
                checkBox.setChecked(true);
                dialog.dismiss();
                break;
            case R.id.apply_two_ok:
                applyTwoListeren.headClick(-1);
                break;
            case R.id.apply_one_user:
                applyTwoListeren.headClick(0);
                break;
            case R.id.apply_one_bank:
                applyTwoListeren.headClick(1);
                break;

        }
    }

    /**
     * 适配器的点击事件
     */
    public interface ApplyTwoListeren {
        void headClick(int position);

        void itemClick(int position);

        void footClick(boolean stat);
    }

    public void setItemClick(ApplyTwoListeren itemClick) {
        this.applyTwoListeren = itemClick;
    }
}